﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EventManager.Model.Factory
{
    public class RepositoryBase<T> : IRepository<T>
        where T : class
    {
        private IDbFactory _dbFactory;

        private EMDbContext _context;
        protected EMDbContext Context
        {
            get { return _context ?? (_dbFactory.Get()); }
        }

        private IDbSet<T> _entity;

        public RepositoryBase(IDbFactory dbFactory)
        {
            _dbFactory = dbFactory;
            _entity = Context.Set<T>();
        }


        public void Add(T entity)
        {
            _entity.Add(entity);
        }

        public T Single(Guid id)
        {
            return _entity.Find(id);
        }

        public void Delete(T entity)
        {
            _entity.Remove(entity);
        }

        public IList<T> GetList()
        {
            return _entity.ToList();
        }

        public void Update(T entity)
        {
            _entity.Attach(entity);
            _context.Entry(entity).State = EntityState.Modified;
        }
    }
}
